package com.service;

import java.util.List;
import com.bean.EasyUiTreeGridBean;
import com.jfinal.plugin.activerecord.Db;
import com.model.Function;
import com.model.Role;
import com.model.RoleFunction;
import com.model.User;

public class UserService {
	public static final UserService service = new UserService();
	public List<User> queryUsers(String login_id, String name){
		
		StringBuffer sqlBF = new StringBuffer();
		sqlBF.append(" SELECT                  ");
		sqlBF.append(" 	a.*,b.name rolename    ");
		sqlBF.append(" FROM                    ");
		sqlBF.append(" 	USER a          ");
		sqlBF.append("   left join role b on ");
		sqlBF.append("  a.roleid = b.roleid ");
		sqlBF.append(" WHERE                   ");
		sqlBF.append(" 	a.login_Id LIKE ?      ");
		sqlBF.append(" AND a.NAME LIKE ?       ");
		List<User> result = User.dao.find(sqlBF.toString(), login_id,name);
		return result;
	}
	
	public boolean getUserByLoginId(String loing_Id){
		List<User> result = User.dao.find("select * from user where login_Id= ?",loing_Id);
		if(result.size()>0){
			return false;
		}
		return true;
	}
	public List<Role> queryRole(String name,String code){
		List<Role> result = Role.dao.find("SELECT * from role where name like ? or code like ? ",name,code);
		return result;
	}
	//检查role是否分配的权限
	public boolean checkRoleHasFunc(String roleid){
		List<RoleFunction> result = RoleFunction.dao.find("SELECT * from role_function where roleid =?",roleid);
		if(result.size()>0){
			return true;
		}
		return false;
	}
	public boolean checkRoleHasUser(String roleid){
		List<User> result = User.dao.find("SELECT * from user where roleid = ?",roleid);
		if(result.size()>0){
			return true;
		}
		return false;
	}
	public boolean checkRoleCodeExists(String code){
		List<Role> result = Role.dao.find("SELECT * from role where code = ?",code);
		if(result.size()>0){
			return true;
		}
		return false;
	}
	public boolean checkRoleCodeExExceptSelf(String code ,String roleid){
		List<Role> result = Role.dao.find("SELECT * from role where code = ? and roleid !=?",code,roleid);
		if(result.size()>0){
			return true;
		}
		return false;
	}
	public List<RoleFunction> getSysFuncByRoleid(String roleid){
		return RoleFunction.dao.find("SELECT * from role_function where roleid = ?",roleid);
	}
	public EasyUiTreeGridBean convertFromFunctionToEasyUiTreeGridBean(Function function) {
		if (function == null) {
			return null;
		} else {
			EasyUiTreeGridBean treeGridBean = new EasyUiTreeGridBean();
			treeGridBean.setId(function.getFunctionid());
			treeGridBean.setText(function.getName());
			
			return treeGridBean;
		}
	}
	public  List<Function> queryBtnListByParentId(String parentid){
		return Function.dao.find("SELECT * from function where parentid = ? and gnlx = '2'",parentid);
	}
	
	public List<Function> getBtnFuncByRoleidParentId(String roleid,String parentid){
		StringBuffer sqlBF = new StringBuffer();
		sqlBF.append(" SELECT                        ");
		sqlBF.append(" 	b.*                          ");
		sqlBF.append(" FROM                          ");
		sqlBF.append(" 	role_function a,             ");
		sqlBF.append(" 	FUNCTION b                   ");
		sqlBF.append(" WHERE                         ");
		sqlBF.append(" 	a.functionid = b.functionid  ");
		sqlBF.append(" AND b.gnlx = '2'              ");
		sqlBF.append(" AND b.parentid = ?            ");
		sqlBF.append(" AND a.roleid = ?              ");
		return Function.dao.find(sqlBF.toString(),parentid,roleid);
	}
	public void deleteRoleFunc(String roleid){
		Db.update("delete  from  role_function where roleid= ?",roleid);
	}
}
